<?php


namespace app\admin\controller;


use app\common\controller\Common;
use app\index\controller\Base;
use think\Db;
use think\Exception;
use think\Session;
use think\Validate;

class News extends Base
{

    //资讯分类
    public function classify()
    {
        //权限查询
        $common = new Common();
        $powerList = $common->showpower();
        $this->assign("powerList",$powerList);

        $where = [];
        $keyword = $community_id = '';
        $status = 0;

        $community_list[0] = ['id' => -1, 'propertyName' => "平台分类"];
        $this->assign("community_list", $community_list);

        if (!empty($_GET['keyword'])) {
            $where['name'] = ['like', "%" . $_GET['keyword'] . "%"];
            $keyword = $_GET['keyword'];
        }
        if (!empty($_GET['status'])) {
            $where['status'] = $_GET['status'];
            $status = $_GET['status'];
        }

        $thisWechat = Session::get("thisWechat");
        $where['uniacid'] = $thisWechat['id'];

        if (!empty($_GET['type'])) {
            try {
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['limit']) ? 10 : $_GET['limit'];
                $start = ($page - 1) * $limit;
                $list = Db::name("admin_news_classify")->order("id desc")->limit($start,$limit)->where($where)->select();
                $count = Db::name("admin_news_classify")->order("id desc")->where($where)->count("*");
                //字段拼接
                foreach ($list as $k => $v) {
                    $propertyName = "平台分类";
                    $list[$k]['propertyName'] = $propertyName;
                }
                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            } catch (Exception $e) {
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;
        }

        $assign = [
            "keyword" => $keyword,
            "community_id" => intval($community_id),
            "status" => $status
        ];
        $this->assign($assign);
        return $this->fetch('classify');
    }


    //添加资讯分类
    public function addClassify()
    {

        $community[0] = ['id' => 0, 'propertyName' => "平台分类"];
        $this->assign("community", $community);

        //启用状态
        $enable_status = Db::name("config_attribute")->where(['key' => "enable_status"])->value("value");
        $enable_status = explode("|", $enable_status);
        foreach ($enable_status as $k => $v) {
            $enable_status[$k + 1] = $v;
        }
        unset($enable_status[0]);
        $this->assign("enable_status", $enable_status);

        if (empty($_GET['id'])) {
            return $this->fetch('addClassify');
        } else {
            //获取基本信息
            $detail = Db::name("admin_news_classify")->where(['id' => $_GET['id']])->find();
            $this->assign("detail", $detail);
            return $this->fetch('editClassify');
        }
    }

    //执行添加修改操作
    public function doClassify()
    {
        try {
            $common = new Common();
            $rule = [
                ["name", "require", "请填写分类名称"],
            ];
            $validate = new Validate($rule);
            $data = $this->request->post();
            $thisWechat = Session::get("thisWechat");
            $data['uniacid'] = $thisWechat['id'];
            if (!$validate->check($data)) {
                throw new Exception($validate->getError());
            }
            //判断分类名称是否重复
            if (!empty($data['id'])) {
                $where['id'] = ['neq', $data['id']];
            }
            $where['name'] = $data['name'];
            $classify = Db::name("admin_news_classify")->where($where)->value("*");
            if (!empty($classify)) {
                throw new Exception("存在相同的分类名称");
            }

            //查询小区名称
            $propertyName = "平台";
            if (empty($data['id'])) {
                $data['addtime'] = time();
                $data['add_id'] = Session::get("id");
                $id = Db::name("admin_news_classify")->insertGetId($data);
                $log['remark'] = "【" . Session::get("user_name") . "】添加新的资讯分类【" . $data['name'] . "】,id【" . $id . "】";
            } else {
                $data['updatetime'] = time();
                $data['update_id'] = Session::get("id");
                $id = $data['id'];
                Db::name("admin_news_classify")->update($data);
                $log['remark'] = "【" . Session::get("user_name") . "】修改资讯分类【" . $data['name'] . "】,id【" . $id . "】";
            }
            $log['controller'] = "News";
            $log['methods'] = "doClassify";
            $common->addLog($log);
            $black['code'] = 0;
            $black['msg'] = "操作成功";
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    //删除分类
    public function delClassify()
    {
        try {
            $id = $_POST['id'];
            $list = Db::name("admin_news_classify")->where(['id' => ['in', $id]])->select();
            $res = Db::name("admin_news_classify")->where(['id' => ['in', $id]])->delete();
            if (empty($res)) {
                throw new Exception("删除失败");
            }
            $common = new Common();
            foreach ($list as $k => $v) {
                //查询小区名称
                $propertyName = "平台";
                $log['remark'] = "【" . Session::get("user_name") . "】删除小区【" . $propertyName . "】的分类【" . $v['name'] . "】";
                $log['controller'] = "News";
                $log['methods'] = "delClassify";
            }
            $common->addLog($log);
            $black['code'] = 0;
            $black['msg'] = "删除成功";
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }


    //资讯列表
    public function index()
    {
        //权限查询
        $common = new Common();
        $powerList = $common->showpower();
        $this->assign("powerList",$powerList);

        $where = [];
        $keyword = $community_id = $classify_id = '';

        if (!empty($_GET['keyword'])) {
            $where['title'] = ['like', "%" . $_GET['keyword'] . "%"];
            $keyword = $_GET['keyword'];
        }

        if (!empty($_GET['classify_id'])) {
            $where['classify_id'] = $_GET['classify_id'];
            $classify_id = $_GET['classify_id'];
        }


        //获取分类
        $classify = Db::name("admin_news_classify")->where(['status' => 1])->select();
        $this->assign("classify", $classify);
        $thisWechat = Session::get("thisWechat");
        $where['uniacid'] = $thisWechat['id'];

        if (!empty($_GET['type'])) {
            try {
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['limit']) ? 10 : $_GET['limit'];
                $start = ($page - 1) * $limit;
                $list = Db::name("admin_news")->order("id desc")->where($where)->limit($start, $limit)->select();
                //字段拼接
                foreach ($list as $k => $v) {
                    $propertyName = "平台新闻";
                    $list[$k]['propertyName'] = $propertyName;
                    $list[$k]['addtime'] = date("Y-m-d H:i:s",$v['addtime']);
                }
                $count = Db::name("admin_news")->order("id desc")->where($where)->count("*");
                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            } catch (Exception $e) {
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;
        }

        $assign = [
            "keyword" => $keyword,
            "community_id" => intval($community_id),
            "classify_id" => intval($classify_id)
        ];
        $this->assign($assign);

        return $this->fetch('index');
    }


    //添加新闻
    public function addNews()
    {
        $community[0] = ['id' => 0, 'propertyName' => "平台新闻"];
        $thisWechat = Session::get("thisWechat");
        $this->assign("community", $community);

        //启用状态
        $enable_status = Db::name("config_attribute")->where(['key' => "enable_status"])->value("value");
        $enable_status = explode("|", $enable_status);
        foreach ($enable_status as $k => $v) {
            $enable_status[$k + 1] = $v;
        }
        unset($enable_status[0]);
        $this->assign("enable_status", $enable_status);

        //获取分类
        $classify = Db::name("admin_news_classify")->where(['status' => 1])->select();
        $this->assign("classify", $classify);

        if (empty($_GET['id'])) {
            return $this->fetch('addNews');
        } else {
            //获取基本信息
            $detail = Db::name("admin_news")->where(['id' => $_GET['id']])->find();
            $this->assign("detail", $detail);
            //解析热门
            $hot = explode(",", $detail['hot']);
            $this->assign("hot", $hot);
            return $this->fetch('editNews');
        }
    }


    //执行添加操作
    public function doNews()
    {
        try {
            $common = new Common();
            $rule = [
                ["classify_id", "require", "请选择分类"],
                ["title", "require", "请填写标题"],
                ["content", "require", "请填写内容"],
            ];
            $validate = new Validate($rule);
            $data = $this->request->post();
            $thisWechat = Session::get("thisWechat");
            $data['uniacid'] = $thisWechat['id'];
            if (!$validate->check($data)) {
                throw new Exception($validate->getError());
            }
            //判断分类名称是否重复
            if (!empty($data['id'])) {
                $where['id'] = ['neq', $data['id']];
            }
            $where['title'] = $data['title'];
            $where['classify_id'] = $data['classify_id'];
            $where['uniacid'] = $thisWechat['id'];
            $news = Db::name("admin_news")->where($where)->value("*");
            if (!empty($news)) {
                throw new Exception("该新闻已存在");
            }

            //查询小区名称
            $propertyName = "平台" ;

            if(!empty($data['hot'])){
                $data['hot'] = implode($data['hot'], ",");
            }

            if (empty($data['id'])) {
                $data['addtime'] = time();
                $data['add_id'] = Session::get("id");
                $data['auto'] = Session::get("auto");
                $id = Db::name("admin_news")->insertGetId($data);
                $log['remark'] = "【" . Session::get("user_name") . "】发布新的新闻资讯【" . $data['title'] . "】,id【" . $id . "】,所属小区【" . $propertyName . "】";
            } else {
                $data['updatetime'] = time();
                $data['update_id'] = Session::get("id");
                $data['auto'] = Session::get("auto");
                $id = $data['id'];
                Db::name("admin_news")->update($data);
                $log['remark'] = "【" . Session::get("user_name") . "】修改新闻资讯【" . $data['title'] . "】,id【" . $id . "】,所属小区【" . $propertyName . "】";
            }
            $log['controller'] = "News";
            $log['methods'] = "doNews";
            $common->addLog($log);
            $black['code'] = 0;
            $black['msg'] = "操作成功";
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    //删除新闻操作
    public function delNews()
    {
        try {
            if (empty($_POST['id'])) {
                throw new Exception("请选择要删除的数据");
            }

            $id = $_POST['id'];
            $list = Db::name("admin_news")->where(['id' => ['in', $id]])->select();
            $res = Db::name("admin_news")->where(['id' => ['in', $id]])->delete();
            if (empty($res)) {
                throw new Exception("删除失败");
            }
            $common = new Common();
            foreach ($list as $k => $v) {
                //查询小区名称
                $propertyName = "平台";
                $log['remark'] = "【" . Session::get("user_name") . "】删除小区【" . $propertyName . "】的新闻【" . $v['title'] . "】";
                $log['controller'] = "News";
                $log['methods'] = "delNews";
            }
            $common->addLog($log);
            $black['code'] = 0;
            $black['msg'] = "删除成功";
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

}